package com.jzy.service.impl;

import com.jzy.entity.Vo.MetaVo;
import com.jzy.entity.Vo.RelationshipVoKey;
import com.jzy.exception.TipException;
import com.jzy.repository.MetaRepository;
import com.jzy.service.IMetaService;
import com.jzy.service.IRelationshipService;
import com.jzy.util.PredicateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class MetaServiceImpl implements IMetaService {

    @Resource
    private MetaRepository metaRepository;

    @Resource
    private IRelationshipService relationshipService;

    @Override
    public List<MetaVo> getMetas(String types) {
        if (StringUtils.isNotBlank(types)) {
            ArrayList<Sort.Order> orders = new ArrayList<>();
            orders.add(new Sort.Order(Sort.Direction.DESC, "sort"));
            orders.add(new Sort.Order(Sort.Direction.DESC, "mid"));
            Sort by = Sort.by(orders);
            Specification<MetaVo> specification = (Specification<MetaVo>) (root, query, criteriaBuilder) -> PredicateUtil.getPredicateByType(types, root, criteriaBuilder);
            return metaRepository.findAll(specification, by);
        }
        return null;
    }

    @Override
    public void saveMetas(Integer cid, String names, String type) {
        if (null == cid) {
            throw new TipException("项目关联id不能为空");
        }
        if (StringUtils.isNotBlank(names) && StringUtils.isNotBlank(type)) {
            String[] nameArr = StringUtils.split(names, ",");
            for (String name : nameArr) {
                this.saveOrUpdate(cid, name, type);
            }
        }
    }

    private void saveOrUpdate(Integer cid, String name, String type) {
        Specification<MetaVo> specification = (Specification<MetaVo>) (root, query, criteriaBuilder) -> {
            ArrayList<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("name"), name));
            predicates.add(criteriaBuilder.equal(root.get("type"), type));
            int size = predicates.size();
            return criteriaBuilder.and(predicates.toArray(new Predicate[size]));
        };
        //查询数据库是否有对应的标签记录
        List<MetaVo> metaVos = metaRepository.findAll(specification);
        //标签记录主键
        int mid;
        MetaVo metas;
        if (metaVos.size() == 1) {
            metas = metaVos.get(0);
            mid = metas.getMid();
        } else if (metaVos.size() > 1) {
            throw new TipException("查询到多条数据");
        } else {
            metas = new MetaVo();
            metas.setSlug(name);
            metas.setName(name);
            metas.setType(type);
            metas.setSort(0);
            metas.setParent(0);
            metaRepository.saveAndFlush(metas);
            //获取添加的记录主键
            mid = metas.getMid();
        }
        if (mid != 0) {
            Long count = relationshipService.countById(cid, mid);
            if (count == 0) {
                RelationshipVoKey relationships = new RelationshipVoKey();
                relationships.setCid(cid);
                relationships.setMid(mid);
                relationshipService.insertVo(relationships);
            }
        }
    }

    @Override
    public void saveMeta(String type, String name, Integer mid) {

    }
}
